﻿@using PagedList.Mvc
@using SelfMadeMillionaire.Core.Enums
@using SelfMadeMillionaire.Web.Infrastructure
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.EmailSend
@model EmailSendList
@{
    ViewBag.Title = "Sent Items";
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}

<div>
    @using (Html.BeginForm("EmailSendList", "EmailSend", FormMethod.Get))
    {
        @Html.HiddenFor(m => m.SortOrder)
        <div id="emailsend-list">
           
            <div class="table-responsive">
                
                @if (Model.SentItem.Any())
                {
                   <table class="table table-bordered tablesorter">
                        <thead>
                            <tr>
                                <th><a href="#">Subject</a></th>
                                <th><a href="#">Subscribers Recipients</a></th>
                                <th><a href="#">Additional Recipients</a></th>
                                <th><a href="#">Sent</a></th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model.SentItem)
                            {
                                 @Html.HiddenFor(m => item.ID)
                                 @Html.HiddenFor(m => item.CreationDate)
                                 @Html.HiddenFor(m => item.Status)
                               <tr>
                                    <td width ="30%">@item.Subject</td>
                                    <td width ="15%">@item.SendTo</td>
                                    <td width ="25%">@item.AdditRecipients</td>
                                   @if (item.Status == (byte) StatusType.Canceled)
                                   {
                                       <td width ="10%" style="color: red">@(item.Message)</td>
                                   }
                                   else
                                   {
                                       <td width ="10%">@(item.Status==(byte)StatusType.Active ? "Has not been sent" : item.SentDate.ToString("MM/dd/yyyy hh:mm:ss tt"))</td>
                                   }
                                   <td width ="10%">
                                        <a href="@Url.Action("ViewSendEmail", "EmailSend", new { id = item.ID, page = Model.SentItem.PageNumber })">View</a> &nbsp;
                                        @if (item.Status == (byte)StatusType.None || item.Status == (byte)StatusType.Canceled)
                                        {
                                            <a onclick="return confirm('Do you want to resend?');" href="@Url.Action("Resend", "EmailSend", new { id = item.ID, page = Model.SentItem.PageNumber })">Resend</a>
                                        }
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
      
                    @Html.PagedListPager(Model.SentItem, page => Url.Action("EmailSendList", new { page, sort = Model.SortOrder }))
                }
                else
                {
                    <p>No records found.</p>
                }
            </div>
        </div>
    }
</div>


<script type="text/javascript">
	$(function () {
	    $("#emailsend-list table").tablesorter({
	        headers: {
	            4: { sorter: false }
	        }
	    });

	    var sortRefresh = true;
	    @if(!string.IsNullOrEmpty(Model.SortOrder))
        {
            <text>
	            sortRefresh = false;
	            setInitSort("emailsend-list", '@(Model.SortOrder)');
            </text>
        }

	    $("#emailsend-list table").on("sortEnd", function (event) {
	        $("#SortOrder").val(event.target.config.sortList);
	        if (sortRefresh)
	            GetList();
	        else
	            sortRefresh = true;
	    });
	});

    function GetList() {
        var sort = $("#@Html.IdFor(m => m.SortOrder)").val();
        window.location.href = "@Url.Action("EmailSendList")" + "?sort=" + sort;
    }
</script>
